1
|
|
|
(function (jQuery,chai) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
jQuery = jQuery && jQuery.hasOwnProperty('default') ? jQuery['default'] : jQuery; |
5
|
|
|
chai = chai && chai.hasOwnProperty('default') ? chai['default'] : chai; |
6
|
|
|
|
7
|
|
|
(function ($, assert) {
|
8
|
|
|
var Callback = (function () {
|
9
|
|
|
function Callback(callback) {
|
10
|
|
|
this.invoked = 0;
|
11
|
|
|
this.callback = callback;
|
12
|
|
|
}
|
13
|
|
|
Callback.prototype.invoke = function () {
|
14
|
|
|
this.callback.apply(this, arguments);
|
15
|
|
|
this.invoked++;
|
16
|
|
|
};
|
17
|
|
|
Callback.prototype.getInvoked = function () {
|
18
|
|
|
return this.invoked;
|
19
|
|
|
};
|
20
|
|
|
return Callback;
|
21
|
|
|
}());
|
22
|
|
|
var Utils = (function () {
|
23
|
|
|
function Utils() {
|
24
|
|
|
}
|
25
|
|
|
Utils.createCallbacks = function () {
|
26
|
|
|
return {
|
27
|
|
|
a: new Callback(function () { }),
|
28
|
|
|
b: new Callback(function () { }),
|
29
|
|
|
div: new Callback(function () { })
|
30
|
|
|
};
|
31
|
|
|
};
|
32
|
|
|
Utils.invokeCallbacks = function (callbacks) {
|
33
|
|
|
return function () {
|
34
|
|
|
callbacks[this.tagName.toLowerCase()].invoke();
|
35
|
|
|
};
|
36
|
|
|
};
|
37
|
|
|
Utils.assertInvoked = function (callbacks, a, b, div, prefix) {
|
38
|
|
|
assert.equal(callbacks.a.getInvoked(), a, (prefix ? prefix + ': ' : '') + 'Callback #a must be invoked ' + a + ' time(s) total');
|
39
|
|
|
assert.equal(callbacks.b.getInvoked(), b, (prefix ? prefix + ': ' : '') + 'Callback #b must be invoked ' + b + ' time(s) total');
|
40
|
|
|
assert.equal(callbacks.div.getInvoked(), div, (prefix ? prefix + ': ' : '') + 'Callback #div must be invoked ' + div + ' time(s) total');
|
41
|
|
|
};
|
42
|
|
|
Utils.wait = function (frames, callback) {
|
43
|
|
|
if (frames <= 0) {
|
44
|
|
|
callback();
|
45
|
|
|
}
|
46
|
|
|
else {
|
47
|
|
|
setTimeout(function () {
|
48
|
|
|
Utils.wait(frames - 1, callback);
|
49
|
|
|
}, 1);
|
50
|
|
|
}
|
51
|
|
|
};
|
52
|
|
|
return Utils;
|
53
|
|
|
}());
|
54
|
|
|
var $body, $fixture, $a, $b, $div;
|
55
|
|
|
beforeEach(function () {
|
56
|
|
|
$fixture = $('<div/>');
|
57
|
|
|
$a = $('<a/>');
|
58
|
|
|
$b = $('<b/>');
|
59
|
|
|
$div = $('<div/>');
|
60
|
|
|
$body = $(document.body);
|
61
|
|
|
$body.append($fixture);
|
62
|
|
|
});
|
63
|
|
|
afterEach(function () {
|
64
|
|
|
$fixture.remove();
|
65
|
|
|
});
|
66
|
|
|
describe('jQuery.Always', function () {
|
67
|
|
|
it('Dependencies have resolved', function () {
|
68
|
|
|
var mo = typeof window.MutationObserver, wmo = typeof window.WebKitMutationObserver;
|
69
|
|
|
assert.isTrue('undefined' !== mo || 'undefined' !== wmo, 'Browser must support mutation observer feature,' +
|
70
|
|
|
' typeof MutationObserver is ' + mo + ', typeof WebKitMutationObserver is ' + wmo);
|
71
|
|
|
});
|
72
|
|
|
it('Plugin has registered', function () {
|
73
|
|
|
assert.isFunction($.fn.always, 'always() must be registered with jQuery');
|
74
|
|
|
assert.isFunction($.fn.never, 'never() must be registered with jQuery');
|
75
|
|
|
});
|
76
|
|
|
it('"Inserted" callbacks are executed for matching selectors (before registration)', function (done) {
|
77
|
|
|
var callbacks = Utils.createCallbacks();
|
78
|
|
|
$fixture
|
79
|
|
|
.append($a)
|
80
|
|
|
.append($b)
|
81
|
|
|
.append($div)
|
82
|
|
|
.always('a, b', Utils.invokeCallbacks(callbacks));
|
83
|
|
|
Utils.wait(1, function () {
|
84
|
|
|
Utils.assertInvoked(callbacks, 1, 1, 0);
|
85
|
|
|
done();
|
86
|
|
|
});
|
87
|
|
|
});
|
88
|
|
|
it('"Inserted" callbacks are executed for matching selectors (after registration)', function (done) {
|
89
|
|
|
var callbacks = Utils.createCallbacks();
|
90
|
|
|
$fixture
|
91
|
|
|
.always('a, b', Utils.invokeCallbacks(callbacks))
|
92
|
|
|
.append($a)
|
93
|
|
|
.append($b)
|
94
|
|
|
.append($div);
|
95
|
|
|
Utils.wait(1, function () {
|
96
|
|
|
Utils.assertInvoked(callbacks, 1, 1, 0);
|
97
|
|
|
done();
|
98
|
|
|
});
|
99
|
|
|
});
|
100
|
|
|
it('"Removed" callbacks are executed for matching selectors', function (done) {
|
101
|
|
|
var callbacks = Utils.createCallbacks();
|
102
|
|
|
$fixture
|
103
|
|
|
.append($a)
|
104
|
|
|
.append($b)
|
105
|
|
|
.append($div)
|
106
|
|
|
.always('a, b', undefined, Utils.invokeCallbacks(callbacks));
|
107
|
|
|
$a.remove();
|
108
|
|
|
$b.remove();
|
109
|
|
|
Utils.wait(1, function () {
|
110
|
|
|
Utils.assertInvoked(callbacks, 1, 1, 0);
|
111
|
|
|
done();
|
112
|
|
|
});
|
113
|
|
|
});
|
114
|
|
|
it('Callbacks are also executed for selectors matching children (deep) of mutated' +
|
115
|
|
|
' elements', function (done) {
|
116
|
|
|
var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks(), $aWrapper = $('<span/>'), $bWrapper = $('<span/>'), $divWrapper = $('<span/>');
|
117
|
|
|
$aWrapper.append($('<span/>').append($a.clone()).append($('<span/>').append($a.clone())));
|
118
|
|
|
$bWrapper.append($('<span/>').append($b.clone()).append($('<span/>').append($b.clone())));
|
119
|
|
|
$divWrapper.append($('<span/>').append($div.clone()).append($('<span/>').append($div.clone())));
|
120
|
|
|
$fixture
|
121
|
|
|
.always('a, b', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
122
|
|
|
.append($aWrapper)
|
123
|
|
|
.append($bWrapper)
|
124
|
|
|
.append($divWrapper);
|
125
|
|
|
Utils.wait(1, function () {
|
126
|
|
|
Utils.assertInvoked(insertedCallbacks, 2, 2, 0);
|
127
|
|
|
Utils.assertInvoked(removedCallbacks, 0, 0, 0);
|
128
|
|
|
$aWrapper.children().children().last().remove();
|
129
|
|
|
$bWrapper.remove();
|
130
|
|
|
$divWrapper.remove();
|
131
|
|
|
Utils.wait(1, function () {
|
132
|
|
|
Utils.assertInvoked(insertedCallbacks, 2, 2, 0);
|
133
|
|
|
Utils.assertInvoked(removedCallbacks, 1, 2, 0);
|
134
|
|
|
done();
|
135
|
|
|
});
|
136
|
|
|
});
|
137
|
|
|
});
|
138
|
|
|
it('Callbacks are not called multiple times for special cases (like wrap() / unwrap()) where child nodes' +
|
139
|
|
|
' are also reported as mutated', function (done) {
|
140
|
|
|
var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks(), $aWrapper = $('<span/>');
|
141
|
|
|
$fixture
|
142
|
|
|
.always('a', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
143
|
|
|
.append($a);
|
144
|
|
|
Utils.wait(1, function () {
|
145
|
|
|
Utils.assertInvoked(insertedCallbacks, 1, 0, 0);
|
146
|
|
|
Utils.assertInvoked(removedCallbacks, 0, 0, 0);
|
147
|
|
|
$a.wrap($aWrapper);
|
148
|
|
|
Utils.wait(1, function () {
|
149
|
|
|
Utils.assertInvoked(insertedCallbacks, 2, 0, 0);
|
150
|
|
|
Utils.assertInvoked(removedCallbacks, 1, 0, 0);
|
151
|
|
|
$a.unwrap();
|
152
|
|
|
Utils.wait(1, function () {
|
153
|
|
|
Utils.assertInvoked(insertedCallbacks, 3, 0, 0);
|
154
|
|
|
Utils.assertInvoked(removedCallbacks, 2, 0, 0);
|
155
|
|
|
done();
|
156
|
|
|
});
|
157
|
|
|
});
|
158
|
|
|
});
|
159
|
|
|
});
|
160
|
|
|
it('Calling never() stops execution of all callbacks', function (done) {
|
161
|
|
|
var insertedCallbacks = Utils.createCallbacks(), removedCallbacks = Utils.createCallbacks();
|
162
|
|
|
$fixture
|
163
|
|
|
.always('a, b', Utils.invokeCallbacks(insertedCallbacks), Utils.invokeCallbacks(removedCallbacks))
|
164
|
|
|
.append($a)
|
165
|
|
|
.append($b)
|
166
|
|
|
.append($div);
|
167
|
|
|
Utils.wait(1, function () {
|
168
|
|
|
Utils.assertInvoked(insertedCallbacks, 1, 1, 0, 'Pre-Never (Inserted)');
|
169
|
|
|
$a.remove();
|
170
|
|
|
$b.remove();
|
171
|
|
|
$div.remove();
|
172
|
|
|
Utils.wait(1, function () {
|
173
|
|
|
Utils.assertInvoked(removedCallbacks, 1, 1, 0, 'Pre-Never (Removed)');
|
174
|
|
|
$fixture
|
175
|
|
|
.never()
|
176
|
|
|
.append($a)
|
177
|
|
|
.append($b)
|
178
|
|
|
.append($div);
|
179
|
|
|
Utils.wait(1, function () {
|
180
|
|
|
Utils.assertInvoked(insertedCallbacks, 1, 1, 0, 'Post-Never (Inserted)');
|
181
|
|
|
$a.remove();
|
182
|
|
|
$b.remove();
|
183
|
|
|
$div.remove();
|
184
|
|
|
Utils.wait(1, function () {
|
185
|
|
|
Utils.assertInvoked(removedCallbacks, 1, 1, 0, 'Post-Never (Removed)');
|
186
|
|
|
done();
|
187
|
|
|
});
|
188
|
|
|
});
|
189
|
|
|
});
|
190
|
|
|
});
|
191
|
|
|
});
|
192
|
|
|
it('Calling never(selector) stops execution of matching callbacks', function (done) {
|
193
|
|
|
var callbacksAB = Utils.createCallbacks(), callbacksDIV = Utils.createCallbacks();
|
194
|
|
|
$fixture
|
195
|
|
|
.always('a, b', Utils.invokeCallbacks(callbacksAB))
|
196
|
|
|
.always('div', Utils.invokeCallbacks(callbacksDIV))
|
197
|
|
|
.append($a)
|
198
|
|
|
.append($b)
|
199
|
|
|
.append($div);
|
200
|
|
|
Utils.wait(1, function () {
|
201
|
|
|
Utils.assertInvoked(callbacksAB, 1, 1, 0, 'Pre-Never (A,B)');
|
202
|
|
|
Utils.assertInvoked(callbacksDIV, 0, 0, 1, 'Pre-Never (DIV)');
|
203
|
|
|
$a.remove();
|
204
|
|
|
$b.remove();
|
205
|
|
|
$div.remove();
|
206
|
|
|
$fixture
|
207
|
|
|
.never('b,a')
|
208
|
|
|
.append($a)
|
209
|
|
|
.append($b)
|
210
|
|
|
.append($div);
|
211
|
|
|
Utils.wait(1, function () {
|
212
|
|
|
Utils.assertInvoked(callbacksAB, 1, 1, 0, 'Post-Never (A,B)');
|
213
|
|
|
Utils.assertInvoked(callbacksDIV, 0, 0, 2, 'Post-Never (DIV)');
|
214
|
|
|
done();
|
215
|
|
|
});
|
216
|
|
|
});
|
217
|
|
|
});
|
218
|
|
|
it('Calling never(selector, insertedCallback, removedCallback) stops execution of exact callback(s)', function (done) {
|
219
|
|
|
var callbacksInsertedA = Utils.createCallbacks(), callbacksInsertedB = Utils.createCallbacks(), callbacksRemovedDIV = Utils.createCallbacks(), _callbacksInsertedA = Utils.invokeCallbacks(callbacksInsertedA), _callbacksInsertedB = Utils.invokeCallbacks(callbacksInsertedB), _callbacksRemovedDIV = Utils.invokeCallbacks(callbacksRemovedDIV);
|
220
|
|
|
$fixture
|
221
|
|
|
.always('a', _callbacksInsertedA)
|
222
|
|
|
.always('b', _callbacksInsertedB)
|
223
|
|
|
.always('div', undefined, _callbacksRemovedDIV)
|
224
|
|
|
.append($a)
|
225
|
|
|
.append($b)
|
226
|
|
|
.append($div);
|
227
|
|
|
$div.remove();
|
228
|
|
|
Utils.wait(1, function () {
|
229
|
|
|
Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Pre-Never (A)');
|
230
|
|
|
Utils.assertInvoked(callbacksInsertedB, 0, 1, 0, 'Pre-Never (B)');
|
231
|
|
|
Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 1, 'Pre-Never (DIV)');
|
232
|
|
|
$a.remove();
|
233
|
|
|
$b.remove();
|
234
|
|
|
$fixture
|
235
|
|
|
.never('a', _callbacksInsertedA)
|
236
|
|
|
.append($a)
|
237
|
|
|
.append($b)
|
238
|
|
|
.append($div);
|
239
|
|
|
$div.remove();
|
240
|
|
|
Utils.wait(1, function () {
|
241
|
|
|
Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Post-Never (A)');
|
242
|
|
|
Utils.assertInvoked(callbacksInsertedB, 0, 2, 0, 'Post-Never (B)');
|
243
|
|
|
Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 2, 'Post-Never (DIV)');
|
244
|
|
|
$a.remove();
|
245
|
|
|
$b.remove();
|
246
|
|
|
$fixture
|
247
|
|
|
.never('div', undefined, _callbacksRemovedDIV)
|
248
|
|
|
.append($a)
|
249
|
|
|
.append($b)
|
250
|
|
|
.append($div);
|
251
|
|
|
$div.remove();
|
252
|
|
|
Utils.wait(1, function () {
|
253
|
|
|
Utils.assertInvoked(callbacksInsertedA, 1, 0, 0, 'Post-Never (A)');
|
254
|
|
|
Utils.assertInvoked(callbacksInsertedB, 0, 3, 0, 'Post-Never (B)');
|
255
|
|
|
Utils.assertInvoked(callbacksRemovedDIV, 0, 0, 2, 'Post-Never (DIV)');
|
256
|
|
|
done();
|
257
|
|
|
});
|
258
|
|
|
});
|
259
|
|
|
});
|
260
|
|
|
});
|
261
|
|
|
it('Adding a second "inserted" callback eligible for immediate execution does not trigger re-execution of the first one', function (done) {
|
262
|
|
|
var callbacks = Utils.createCallbacks();
|
263
|
|
|
$fixture
|
264
|
|
|
.append($a.attr('data-ab', ''))
|
265
|
|
|
.append($b.attr('data-ab', ''))
|
266
|
|
|
.append($div)
|
267
|
|
|
.always('a', Utils.invokeCallbacks(callbacks))
|
268
|
|
|
.always('[data-ab]', Utils.invokeCallbacks(callbacks));
|
269
|
|
|
Utils.wait(1, function () {
|
270
|
|
|
Utils.assertInvoked(callbacks, 2, 1, 0);
|
271
|
|
|
done();
|
272
|
|
|
});
|
273
|
|
|
});
|
274
|
|
|
});
|
275
|
|
|
})(jQuery, chai.assert); |
276
|
|
|
|
277
|
|
|
}(jQuery,chai)); |
|
|
|
|
278
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.